package com.example.dllo.my36kr.message.enter;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.my.MyFragment;
import com.example.dllo.my36kr.tools.BaseFragment;
import com.example.dllo.my36kr.tools.StaticUrl;

import org.greenrobot.eventbus.EventBus;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by dllo on 16/8/27.
 */
public class EnterFragment extends BaseFragment {

    private String phone;
    private String cipher;
    private Button enterBnt;
    private EditText phoneEt;
    private EditText cipherEt;

    @Override
    protected int setLayout() {
        return R.layout.fragment_enter;
    }

    @Override
    protected void initView() {
        phoneEt = bindView(R.id.et_fragment_enter_phone);
        cipherEt = bindView(R.id.et_fragment_enter_cipher);
        enterBnt = bindView(R.id.bnt_fragment_enter_enter);
        enterBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StaticUrl.createLoadingDialog(getContext());
                phone = phoneEt.getText().toString();
                cipher = cipherEt.getText().toString();
                final BmobUser bmobUser = new BmobUser();//登录bmob
                bmobUser.setUsername(phone);
                bmobUser.setPassword(cipher);
                bmobUser.login(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if (e == null) {

                           Intent intent = new Intent();
                            intent.setAction("phone");
                            intent.putExtra("phone",phone);
                            getContext().sendBroadcast(intent);
                             Toast.makeText(getContext(), "登入成功", Toast.LENGTH_SHORT).show();
                            getActivity().finish();
                        } else {
                            Toast.makeText(getContext(), "用户名或者密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });

    }

    @Override
    protected void initData() {


    }
}
